package net.diaowen.common.utils;

import net.diaowen.dwsurvey.entity.Prize;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.Map.Entry;


public class Arithmetic {
 // 放大倍数
    private static final int mulriple = 1000;

    List<Prize>  prizes = new ArrayList<Prize>();

    public  List<Prize> getPrizes() {
        return prizes;
    }

    public  void setPrizes(List<Prize> prizes) {
        prizes = prizes;
    }

    public int pay(List<Prize> prizes) {
            int lastScope = 0;
            // 洗牌，打乱奖品次序
            Collections.shuffle(prizes);
            Map<Integer, int[]> prizeScopes = new HashMap<Integer, int[]>();
            Map<Integer, Integer> prizeQuantity = new HashMap<Integer, Integer>();
            for (Prize prize : prizes) {
                    int prizeId = prize.getPrizeId();
                    // 划分区间
                    int currentScope = lastScope + prize.getProbability().multiply(new BigDecimal(mulriple)).intValue();
                    prizeScopes.put(prizeId, new int[] { lastScope + 1, currentScope });
                    prizeQuantity.put(prizeId, prize.getQuantity());
                    lastScope = currentScope;
             }

           int luckyNumber = new Random().nextInt(mulriple);
           int luckyPrizeId = 0;

            // 获取1-1000000之间的一个随机数
            // 查找随机数所在的区间
            if ((null != prizeScopes) && !prizeScopes.isEmpty()) {
                Set<Entry<Integer, int[]>> entrySets = prizeScopes.entrySet();
                for (Map.Entry<Integer, int[]> m : entrySets) {
                    int key = m.getKey();
                    if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1] && prizeQuantity.get(key) > 0) {
                        luckyPrizeId = key;
                        break;
                    }
                }

            }


        if (luckyPrizeId > 0) {
            for (Prize prize : prizes) {
               if(luckyPrizeId==prize.getPrizeId().intValue()){
                   prize.setQuantity(prize.getQuantity()-1);
                   break;
                };
            }


        }


       return luckyPrizeId;
   }


    /**
     *
     * @param prizeNum  包括未中奖的总奖个数
     * @param prizes
     * @return
     */
   public   Integer[] getPrizeList(int prizeNum,List<Prize> prizes ){

       Integer[] prizeList =new Integer[prizeNum];
       Integer[] prizeNumList = new Integer[prizes.size()];

       for(int i=0;i<prizes.size();i++){
           prizeNumList[i]=0;
       }


       setPrizes(prizes);

       for (int i = 0; i < prizeNum; i++) {

           int prizeId = pay(prizes);
           prizeList[i]=prizeId;

       }



       int totalLeft = 0;
       int totalPrize = 0;

       for(int j=0;j<prizeNum;j++){
           if(prizeList[j]>0 && prizeList[j]<10 )
             prizeNumList[prizeList[j]-1] ++;
           else{
               totalLeft++;
           }
       }

       System.out.println("抽奖次数" + prizeNum);

       for(int i=0;i<prizeNumList.length;i++){
           System.out.println("prize"+i+"中奖次数" + prizeNumList[i]);
       }

       System.out.println(totalPrize+"总剩余"+totalLeft);

       int leftPrizeNum =0;

       for (Prize prize : prizes) {
           leftPrizeNum =prize.getQuantity().intValue();
           System.out.println(prize.getPrizeId()+"剩余"+leftPrizeNum);
           if(leftPrizeNum>0) {
               for (int i = 0; i < leftPrizeNum; i++) {
                   for(int j=0;j<prizeNum;j++){
                          if(prizeList[j]==0){
                              prizeList[j] =prize.getPrizeId();
                              System.out.println("在第"+j+"补充一个："+prize.getPrizeId());
                              break;
                          }
                   }
               }
           }

       }

       return prizeList;

   }

    public  static  void main(String[] args) throws IOException {


    }
}
